package com.yu.crm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yu.crm.domain.dto.CrmRoleMenuReq;
import com.yu.crm.domain.pojo.CrmMenu;
import com.yu.crm.domain.pojo.CrmRoleMenuLink;
import com.yu.crm.mapper.CrmRoleMenuLinkMapper;
import com.yu.crm.service.CrmMenuService;
import com.yu.crm.service.CrmRoleMenuLinkService;
import com.yxzz.common.config.UserCacheManager;
import com.yxzz.common.helper.ExceptionHelpers;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 角色菜单权限表 服务实现类
 * </p>
 *
 * @author donnie-bin
 * @since 2019-06-10
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class CrmRoleMenuLinkServiceImpl extends ServiceImpl<CrmRoleMenuLinkMapper, CrmRoleMenuLink> implements CrmRoleMenuLinkService {

    @Override
    public List<CrmMenu> getMenusByRoleId(Long roleId) {
        return baseMapper.getMenusByRoleId(roleId);
    }

    @Override
    public void updateBatch(CrmRoleMenuReq crmRoleMenuReq) {
        Long roleId = crmRoleMenuReq.getRoleId();
        QueryWrapper<CrmRoleMenuLink> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_id", roleId);
        super.remove(queryWrapper);

        List<CrmRoleMenuLink> list = new ArrayList<>();
        crmRoleMenuReq.getMenus().forEach(menu -> {
            CrmRoleMenuLink cml = new CrmRoleMenuLink();
            cml.setRoleId(roleId);
            cml.setMenuId(menu.getId());
            list.add(cml);
        });
        if (list.size() > 0) {
            ExceptionHelpers.checkData(super.saveBatch(list), "保存失败");
        }
    }

}
